//
//  TFVoteListView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFVoteListView: UIView,UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "votelistCell") as! TFVoteListCell
   
        cell.tf_height = 98;
        cell.tf_width = self.frame.size.width;
        let model = self.dataSource[indexPath.row] as! TFVoteData
        cell.model = model;
        cell.selectionStyle = .none
        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let  model = self.dataSource[indexPath.row] as! TFVoteData

        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFVoteViewDelegate.voteDidSelectRowAtIndexPath(_:))))! {
            self.delegate?.voteDidSelectRowAtIndexPath(model)
        }
    
 
    }
    
    /** 代理对象 */

    @objc public weak var delegate : TFVoteViewDelegate?
    @objc public var closeBtn:UIButton!

    //显示投票列表窗口,yes 为显示
    @objc public var isShow:Bool = false
    
    var imgView:UIImageView!; //老师头像

    var titleLab:UILabel!

    var tableview:UITableView!

    var dataSource:NSMutableArray = NSMutableArray.init()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.imgView = UIImageView.init()
        self.imgView.image = UIImage.init(named: "HTLive.bundle/head photo")
        addSubview(self.imgView)

        
        self.titleLab = UILabel.init()
        self.titleLab.text = "老师发起一个投票";
        self.titleLab.textColor = UIColor(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
        self.titleLab.font = UIFont.PingFang(size: 14)
        addSubview(self.titleLab)
        
        self.closeBtn = UIButton.init()
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        addSubview(self.closeBtn)

        self.tableview = UITableView.init()
        self.tableview.backgroundColor = UIColor.clear
        self.tableview.separatorStyle = .none
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        addSubview(self.tableview)
        self.tableview.rowHeight = 98;
        self.tableview.register(TFVoteListCell.self, forCellReuseIdentifier: "votelistCell")
    
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.imgView.frame = CGRect(x: 16, y: 16, width: 22, height: 22)
        self.titleLab.frame = CGRect(x: self.imgView.frame.maxX+8, y: 17, width: self.frame.size.width-80, height: 20)
       
        self.closeBtn.frame = CGRect(x: self.frame.size.width-10-32, y: 10, width: 32, height: 32)
    
        self.tableview.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+25, width: self.frame.size.width, height: self.frame.size.height-25-self.titleLab.frame.maxY)
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   @objc public func updataVote(_ result:NSArray) {
      
        self.dataSource.removeAllObjects()
        for data in result {
            self.dataSource .add(data as! TFVoteData)
        }
        self.tableview.reloadData()
      
    }  

}

@objc protocol TFVoteViewDelegate : NSObjectProtocol {

    
    func voteDidSelectRowAtIndexPath(_ model : TFVoteData)
}
